package com.foreknow.demo16;

import com.foreknow.demo12.User;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ListDemo {

    public void m1(List<User> list){
        for (int i=0;i<list.size();i++){
            User user = list.get(i);
            System.out.println(user.getName());
        }
    }
    public List<User> m2(){
        List<User> list = new ArrayList<>();
        User user = new User();
        list.add(user);
        return list;
    }
    public static void main(String[] args) {
        //1.创建一个集合容器List  如何约束集合中的元素的类型? 可以使用泛型<E>
        List<String> list = new ArrayList<>();
        //2.向容器(list)中添加元素
       // list.add(1);//Integer
        //list.add(3.14);//Double
        //list.add(true);
        list.add("fender");
        list.add("熊大");
        list.add("熊二");
        //3.获取到集合当中的某一个元素
        Object a= list.get(1);
        System.out.print("输出元素个数:"+list.size());
        System.out.print(a);
        System.out.println();
        //4.遍历List集合
        for (int i=0;i<list.size();i++){
            String str = list.get(i);
            System.out.println(str);
        }
        System.out.println("--------");
        for (String str:list){
            System.out.println(str);
        }

        List<User> list1 = new ArrayList<>();
        User u1 = new User();
        u1.setName("fender");
        User u2 = new User();
        u2.setName("fender");
        User u3 = new User();
        u3.setName("fender");
        list1.add(u1);
        list1.add(u2);
        list1.add(u3);
        for (User user:list1){
            System.out.println(user.getName());

        }

        System.out.println("------迭代器Iterator接口--------");
        Iterator<User> it =  list1.iterator();
        while (it.hasNext()){
           User user= it.next();
            System.out.println(user.getName());

        }


        ListDemo listDemo = new ListDemo();
        listDemo.m1(list1);
       List<User> u= listDemo.m2();
       for (User s:u){
           System.out.println(s.getName());
       }
    }

}
